package com.tanhua.server.controller;

import com.tanhua.model.vo.PageResult;
import com.tanhua.server.interceptor.UserHolder;
import com.tanhua.server.service.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.Map;

@RestController
@RequestMapping("/smallVideos")
@Slf4j
public class SmallVideosController {

    @Autowired
    private SmallVideosService videosService;

    @Autowired
    private CommentsService commentsService;

    @Autowired
    private CommentLikeService commentLikeService;

    @Autowired
    private MqMessageService mqMessageService;

    /**
     * 发布视频
     *  接口路径：POST
     *  请求参数：
     *      videoThumbnail：封面图
     *      videoFile：视频文件
     */
    @PostMapping
    public ResponseEntity saveVideos(MultipartFile videoThumbnail, MultipartFile videoFile) throws IOException {
        videosService.saveVideos(videoThumbnail,videoFile);
        return ResponseEntity.ok(null);
    }

    /**
     * 视频列表
     */
    @GetMapping
    public ResponseEntity queryVideoList(@RequestParam(defaultValue = "1")  Integer page,
                                         @RequestParam(defaultValue = "10") Integer pagesize) {
        PageResult result = videosService.queryVideoList(page, pagesize);
        return ResponseEntity.ok(result);
    }
    /**
     * 视频点赞功能
     *
     * @param videoId
     * @return
     */
    @PostMapping("{id}/like")
    public ResponseEntity likeVideo(@PathVariable("id") String videoId) {
        Integer count = commentsService.likeVideo(videoId);
        return ResponseEntity.ok(count);
    }

    /**
     * 视频取消点赞
     *
     * @param videoId
     * @return
     */
    @PostMapping("{id}/dislike")
    public ResponseEntity disLikeVideo(@PathVariable("id") String videoId) {
        Integer count = commentsService.disLikeVideo(videoId);
        return ResponseEntity.ok(count);
    }

    /**
     * 视频关注
     *
     * @param userId 视频所属用户userId
     * @return
     */
    @PostMapping("/{uid}/userFocus")
    public ResponseEntity userFocus(@PathVariable("uid") Long userId) {
        videosService.likeUser(userId);
        return ResponseEntity.ok(null);
    }

    /**
     * 取消关注视频用户
     *
     * @param userId
     * @return
     */
    @PostMapping("/{uid}/userUnFocus")
    public ResponseEntity userUnFocus(@PathVariable("uid") Long userId) {
        videosService.notLikeUser(userId);
        return ResponseEntity.ok(null);
    }

    /**
     * 视频评论列表
     *
     * @param videoId
     * @return
     */
    @GetMapping("/{id}/comments")
    public ResponseEntity getComments(@RequestParam(defaultValue = "1") Integer page
            , @RequestParam(defaultValue = "10") Integer pagesize
            , @PathVariable("id") String videoId) {
        log.info("视频id为:" + videoId);
        PageResult pr = commentsService.findComments(videoId, page, pagesize);
//        PageResult pr = videosService.getComments(page, pagesize, videoId);
        return ResponseEntity.ok(pr);
    }

    /**
     * 发布视频评论
     *
     * @param videoId
     * @return
     */
    @PostMapping("/{id}/comments")
    public ResponseEntity saveComments(@PathVariable("id") String videoId, @RequestBody Map map) {
        String comment = (String) map.get("comment");
        commentsService.saveVideoComments(videoId, comment);
        mqMessageService.sendLogMessage(UserHolder.getUserId(), "0304", "video", videoId);
        return ResponseEntity.ok(null);
    }

    /**
     * 视频评论点赞
     *
     * @param commentId
     * @return
     */
    @PostMapping("/comments/{id}/like")
    public ResponseEntity likeComment(@PathVariable("id") String commentId) {
        Integer count = commentLikeService.likeComment(commentId);
        return ResponseEntity.ok(count);
    }

    /**
     * 视频评论取消点赞
     *
     * @param commentId
     * @return
     */
    @PostMapping("/comments/{id}/dislike")
    public ResponseEntity disLikeComment(@PathVariable("id") String commentId) {
        Integer count = commentLikeService.disLikeComment(commentId);
        return ResponseEntity.ok(count);
    }
}